using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;


namespace Boids2D
{
    class Boid
    {
        Vector2 velocity;
        Vector2 position;
        Rectangle boundingBox;
        Mesh mesh;
        Color color;



        internal Mesh Mesh
        {
            get { return mesh; }
            set { mesh = value; }
        }

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }
        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
        public Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;
               // boundingBox.X = (int)(position.X - (boundingBox.Width / 2));
               // boundingBox.Y = (int)(position.Y - (boundingBox.Height / 2));

            }
        }

        public Boid()
        {

            InitMesh();
        }

        public Boid(int xIn, int yIn, int widthIn, int heightIn)
        {
            
            InitMesh();
            boundingBox = new Rectangle(xIn, yIn, widthIn, heightIn);
        }


        private void InitMesh()
        {
            mesh = new Mesh();
            Triangle t = new Triangle();
            float scale = 1;
            //a single up pointing triangle
            Color tip = Color.Black;
            Color back = Color.FromArgb(100, 200, 230);

            t.V1 = new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored(0f * scale, -5f * scale, 0f * scale, 1f, tip.ToArgb());
            t.V2 = new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored(3f * scale, 3 * scale, 0f * scale, 1f, back.ToArgb());
            t.V3 = new Microsoft.DirectX.Direct3D.CustomVertex.TransformedColored(-3f * scale, 3f * scale, 0f * scale, 1f, back.ToArgb());
            mesh.Triangles.Add(t);
        }


        /*
        internal void Draw(Graphics g)
        {
            g.FillEllipse(Brushes.Chocolate, boundingBox);
        }
        */

        internal void Draw(Microsoft.DirectX.Direct3D.Device device)
        {
            double rotation = Math.Atan2((double)this.velocity.X,-(double)this.velocity.Y);
            //rotation = .25*Math.PI;
            foreach (Triangle tri in mesh.Triangles)
            {
                
                Triangle tempTri = new Triangle();
                for (int i = 0; i < tri.Vertices.Length; i++)
                {
                    /*
                    float x = tri.Vertices[i].X + (float)position.X;
                    float y = tri.Vertices[i].Y + (float)position.Y;
                    float z = 0;// tri.Vertices[i].Z + (float)position.Z;
                    */

                    //rotated vertices
                    //x'=x*cos(angle) - y*sin(angle)
                    //y'=x*sin(angle) + y*cos(angle)

                    float x = (float)(tri.Vertices[i].X * Math.Cos(rotation) - tri.Vertices[i].Y * Math.Sin(rotation)) + (float)position.X;
                    float y = (float)(tri.Vertices[i].X * Math.Sin(rotation) + tri.Vertices[i].Y * Math.Cos(rotation)) + (float)position.Y;
                    float z = 0;// tri.Vertices[i].Z + (float)position.Z;


                    tempTri.Vertices[i] = new CustomVertex.TransformedColored(x, y, z, 1f, tri.Vertices[i].Color);

                }


                device.BeginScene();
                device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, tempTri.Vertices);
                device.EndScene();
            }
        }
    }
}
